#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

//生成正弦波信号
vector<double> generateSineWave(double freq, double duration, double sampleRate)
{
    vector<double> signal;
    int numSamples = static_cast<int>(duration * sampleRate);
    for (int i = 0; i < numSamples; i++)
	{
        double t = i / sampleRate;
        signal.push_back(sin(2 * M_PI * freq * t));
    }
    return signal;
}

//采样信号（按给定采样率）
void plotInTerminal(const vector<double>& signal, int height = 20)
{
    int width = signal.size();
    for (int y = height; y >= -height; y--)
	{
        for (int x = 0; x < width; x++)
		{
            double scaledY = signal[x] * height;
            if (abs(scaledY - y) < 0.5) cout << "*";
            else cout << " ";
        }
        cout << endl;
    }
}

int main()
{
    double signalFreq = 5.0;
    double duration = 1.0;
    double sampleRate = 1000.0;
    auto signal = generateSineWave(signalFreq, duration, sampleRate);

    //采样信号（低于Nyquist）
    double lowSampleRate = 1.9 * signalFreq;//9.5Hz
    vector<double> sampledSignal;
    for (int i = 0; i < signal.size(); i += static_cast<int>(sampleRate / lowSampleRate))
	{
        sampledSignal.push_back(signal[i]);
    }

    cout << "Original Signal (5Hz):" << endl;
    plotInTerminal(signal);

    cout << "\nSampled Signal (9.5Hz < 2*5Hz):" << endl;
    plotInTerminal(sampledSignal);

    return 0;
}